(function(){
    var restfulClient={};

    var appName="http://jjp.zhiyuzhuli.com";
    var backendUrl=appName+"/ee-wechat";
    window.rsClient=restfulClient;
    window.restfulClient=restfulClient;

    restfulClient.hostUrl="";


    restfulClient.defaultOption={
        before:function(){
            //console.log("");
        },
        failed:function(failedInfo){
                alert(failedInfo.errorMesg);
        }
    };


    /*@param String name : 要获取的参数名
     * window.location.search="?healthId=18&from=groupmessage&isappinstalled=1&n";
     * 用法:var id=FJS.getUrlParams("healthId");//获取healthId的值
     *JS解析URL的参数:获得指定名的参数
     */
    restfulClient.getUrlParams = function(name){
        var reg = new RegExp("(^|&)"+ name +"=([^&]*)(&|$)");
        var r = window.location.search.substr(1).match(reg);
        if(r!=null)return  decodeURI(r[2]); return null;
    };

    /**
     * 保存方法，如果data 有id属性，就是put，否则就是post
     * @param url
     * @param data
     * @param success
     * @param before
     * @param failed 自定义callback，服务器返回data 有failed字段，就会call这个方法
     */
    restfulClient.save=function(url,data,success,option){
        var syncOption={
            "httpType":"POST",
            "success":success,
            "data":data,
            "option":option || restfulClient.defaultOption
        };
        if(data.id){
            syncOption.httpType="PUT";
        }
        restfulClient.syncData(url,syncOption);
    };
    /**
     *
     * @param url
     * @param success
     * @param failed
     */
    restfulClient.get=function(url,success,option){
        var syncOption={
            "httpType":"GET",
            "success":success,
            "option":option || restfulClient.defaultOption
        };
        restfulClient.syncData(url,syncOption);
    };

    /**
     *
     * @param url
     * @param success
     * @param failed
     */
    restfulClient.remove = function(url,success,option){
        var syncOption={
            "httpType":"DELETE",
            "success":success,
            "option":option || restfulClient.defaultOption
        };
        restfulClient.syncData(url,syncOption);
    };

    restfulClient.syncData=function(url,syncOption){
        var ajaxOption={
            url: backendUrl+url,
            type:syncOption.httpType,
            contentType:"application/json;charset=utf-8",
            headers:{"token":restfulClient.getToken()},
            cache:false,
            async:syncOption.option.hasOwnProperty("async") ? syncOption.option.async:true,
            beforeSend:syncOption.option.before,
            success: function(data) {
                if(data.success){
                    syncOption.success(data.success);
                }
                if(data.failed){
                    if(!restfulClient.globalException(data)){
                        syncOption.option.failed(data.failed);
                    }
                }
            }
        };
        if(syncOption.httpType=="POST" || syncOption.httpType=="PUT"){
            ajaxOption.data=JSON.stringify(syncOption.data)
        }
            $.ajax(ajaxOption);

    };

    restfulClient.globalException=function(failedData){
        if(failedData.errorType == 3) {
            return true;
        }
        return false;
    };
    restfulClient.getToken=function(){
       return "";
    }
})();